#include "stdafx.h"
#include "gui/main/control/session_item_helper.h"
#include "duilib/Utils/MultiLangSupport.h"
#include "module/emoji/emoji_info.h"
#include "util/net_call_helper.h"

namespace nim_comp
{
	void SessionItemHelper::GetMsgContent(const nim::SessionData &msg, std::wstring &show_text)
	{
		ui::MutiLanSupport* mls = ui:: MutiLanSupport::GetInstance();
		if (msg.msg_type_ == nim::kNIMMessageTypeText)
		{
			show_text = nbase::UTF8ToUTF16(msg.msg_content_);
			if (ui::GlobalManager::GetLanguageSetting().m_enumType == ui::LanguageType::American_English)
			{
				std::vector<emoji::Emoticon> vec;
				emoji::GetEmoticon(vec);
				for (auto it : vec)
				{
					nbase::StringReplaceAll(it.tag, L"[" + it.tip + L"]", show_text);
				}
			}
			else
			{
				for (size_t i = 0; i < show_text.size();) {
					wchar_t em = show_text.at(i);
					if (em == 0xd83d || em == 0xd83e) {
						std::wstring emo;
						SessionItemHelper::ConvertEmojiText(show_text.at(i + 1), emo);
						show_text.replace(i, 2, emo);
						i += emo.length();
					}
					else if (em == 0x261d || em == 0x270a || em == 0x270b || em == 0x270c) {
						std::wstring emo;
						SessionItemHelper::ConvertEmojiText(em, emo);
						show_text.replace(i, 1, emo);
						i += emo.length();
					}
					else i += 1;
				}
			}
 		}
		else if (msg.msg_type_ == nim::kNIMMessageTypeImage)
		{
			Json::Value msg_attach;
			if (Json::Reader().parse(msg.msg_attach_, msg_attach) && msg_attach.isObject() && msg_attach.isMember("description"))
				show_text = nbase::UTF8ToUTF16(msg_attach["description"].asString());
			if (show_text.empty())
				show_text = mls->GetStringViaID(L"STRID_SESSION_ITEM_MSG_TYPE_IMAGE");
		}
		else if (msg.msg_type_ == nim::kNIMMessageTypeAudio)
		{
			show_text = mls->GetStringViaID(L"STRID_SESSION_ITEM_MSG_TYPE_AUDIO");
		}
		else if (msg.msg_type_ == nim::kNIMMessageTypeVideo)
		{
			show_text = mls->GetStringViaID(L"STRID_SESSION_ITEM_MSG_TYPE_VIDEO");
		}
		else if (msg.msg_type_ == nim::kNIMMessageTypeFile)
		{
			show_text = mls->GetStringViaID(L"STRID_SESSION_ITEM_MSG_TYPE_FILE");
		}
		else if (msg.msg_type_ == nim::kNIMMessageTypeLocation)
		{
			show_text = mls->GetStringViaID(L"STRID_SESSION_ITEM_MSG_TYPE_LOCATION");
		}
		else if (msg.msg_type_ == nim::kNIMMessageTypeNotification)
		{
			GetNotifyMsg(msg.msg_attach_, msg.msg_sender_accid_, "", show_text, msg.id_);
		}
		else if (msg.msg_type_ == nim::kNIMMessageTypeCustom)
		{
			show_text = GetCustomMsg(msg.msg_sender_accid_, msg.msg_attach_);
		}
		else if (msg.msg_type_ == nim::kNIMMessageTypeTips)
		{
			show_text = mls->GetStringViaID(L"STRID_SESSION_ITEM_MSG_TYPE_NOTIFY");
		}
		else if (msg.msg_type_ == nim::kNIMMessageTypeUnknown)
		{
			show_text = mls->GetStringViaID(L"STRID_SESSION_ITEM_MSG_TYPE_UNDEFINED");
		}
		else if (msg.msg_type_ == nim::kNIMMessageTypeRobot)
		{
			bool out_msg = false;
			Json::Value values;
			Json::Reader reader;
			if (reader.parse(msg.msg_attach_, values) && values.isObject() && values.isObject())
				out_msg = values[nim::kNIMBotRobotReceivedMsgKeyMsgOut].asBool();
			if (!out_msg)
				show_text = nbase::UTF8ToUTF16(msg.msg_content_);
			else
				show_text = mls->GetStringViaID(L"STRID_SESSION_ITEM_MSG_TYPE_ROBOT");
		}
		else if (msg.msg_type_ == nim::kNIMMessageTypeG2NetCall)
		{
			show_text = ParseNetCallMsg(msg);
		}
		else
		{
			Json::Value extern_info;
			if (Json::Reader().parse(msg.msg_attach_, extern_info) && extern_info.isObject() && extern_info.isMember("virtual_local_message") && extern_info["virtual_local_message"].asBool())
			{
				show_text = L"";
			}
			else
			{
				show_text = mls->GetStringViaID(L"STRID_SESSION_ITEM_MSG_TYPE_UNKNOWN");
				QLOG_WAR(L"unknown msg: id_type={0}_{1} msg_type={2}") << msg.id_ << msg.type_ << msg.msg_type_;
			}
		}
	}

	void SessionItemHelper::ConvertEmojiText(const std::wstring& msg, std::wstring& emo) {
		emo = msg;
		std::vector<emoji::Emoticon> vec;
		emoji::GetEmoticon(vec);
		for (auto it : vec)
		{
			UTF32Char unicode = it.unicode;
			std::wstring emojText = nbase::UTF32ToUTF16(&unicode, 1);
			nbase::StringReplaceAll(it.tag, emojText, emo);
		}
	}

	void SessionItemHelper::ConvertEmojiText(wchar_t ch, std::wstring& emo)
	{
		switch (ch) {
		case 0x261d: emo = L"[第一]"; break;
		case 0x270a: emo = L"[拳头]"; break;
		case 0x270b: emo = L"[手掌]"; break;
		case 0x270c: emo = L"[剪刀]"; break;
		case 0xdc35: emo = L"[猴子]"; break;
		case 0xdc37: emo = L"[猪头]"; break;
		case 0xdc4a: emo = L"[打你]"; break;
		case 0xdc4b: emo = L"[拜拜]"; break;
		case 0xdc4c: emo = L"[好的]"; break;
		case 0xdc4d: emo = L"[赞]"; break;
		case 0xdc4e: emo = L"[低级]"; break;
		case 0xdc4f: emo = L"[鼓掌]"; break;
		case 0xdc93: emo = L"[心动]"; break;
		case 0xdc94: emo = L"[心碎]"; break;
		case 0xdca3: emo = L"[炸弹]"; break;
		case 0xdca4: emo = L"[睡觉]"; break;
		case 0xdca8: emo = L"[筋斗云]"; break;
		case 0xdca9: emo = L"[便便]"; break;
		case 0xdcaa: emo = L"[给力]"; break;
		case 0xdd17: emo = L"[偷笑]"; break;
		case 0xde00: emo = L"[大笑]"; break;
		case 0xde02: emo = L"[笑哭]"; break;
		case 0xde05: emo = L"[流汗]"; break;
		case 0xde06: emo = L"[狂喜]"; break;
		case 0xde07: emo = L"[天使]"; break;
		case 0xde08: emo = L"[鬼怪]"; break;
		case 0xde09: emo = L"[眯眼]"; break;
		case 0xde0a: emo = L"[可爱]"; break;
		case 0xde0c: emo = L"[开心]"; break;
		case 0xde0d: emo = L"[色]"; break;
		case 0xde0e: emo = L"[酷]"; break;
		case 0xde0f: emo = L"[奸笑]"; break;
		case 0xde10: emo = L"[OK]"; break;
		case 0xde11: emo = L"[好吧]"; break;
		case 0xde12: emo = L"[鄙视]"; break;
		case 0xde13: emo = L"[汗]"; break;
		case 0xde14: emo = L"[难受]"; break;
		case 0xde15: emo = L"[不屑]"; break;
		case 0xde16: emo = L"[不舒服]"; break;
		case 0xde1a: emo = L"[亲]"; break;
		case 0xde1b: emo = L"[伸舌头]"; break;
		case 0xde1c: emo = L"[鬼脸]"; break;
		case 0xde1d: emo = L"[偷笑]"; break;
		case 0xde1f: emo = L"[皱眉]"; break;
		case 0xde20: emo = L"[愤怒]"; break;
		case 0xde21: emo = L"[发怒]"; break;
		case 0xde24: emo = L"[生气]"; break;
		case 0xde25: emo = L"[流泪]"; break;
		case 0xde28: emo = L"[尴尬]"; break;
		case 0xde29: emo = L"[烦躁]"; break;
		case 0xde2c: emo = L"[龇牙]"; break;
		case 0xde2d: emo = L"[大哭]"; break;
		case 0xde2e: emo = L"[惊讶]"; break;
		case 0xde30: emo = L"[汗]"; break;
		case 0xde31: emo = L"[惊恐]"; break;
		case 0xde32: emo = L"[两眼冒星]"; break;
		case 0xde33: emo = L"[害羞]"; break;
		case 0xde34: emo = L"[睡着]"; break;
		case 0xde35: emo = L"[冒星]"; break;
		case 0xde37: emo = L"[口罩]"; break;
		case 0xde41: emo = L"[不高兴]"; break;
		case 0xde42: emo = L"[可爱]"; break;
		case 0xde45: emo = L"[不要]"; break;
		case 0xde46: emo = L"[举着]"; break;
		case 0xde47: emo = L"[思考]"; break;
		case 0xde48: emo = L"[不看]"; break;
		case 0xde49: emo = L"[不听]"; break;
		case 0xde4a: emo = L"[不说]"; break;
		case 0xde4b: emo = L"[招手]"; break;
		case 0xde4f: emo = L"[阿弥陀佛]"; break;
		case 0xde80: emo = L"[火箭]"; break;
		case 0xde91: emo = L"[救护车]"; break;
		default: emo = L"[表情]"; break;
		}
	}
}